import React from 'react';
import { Upload, message } from 'antd';
import { InboxOutlined } from '@ant-design/icons';
import type { UploadProps } from 'antd';

const { Dragger } = Upload;

interface FileUploaderProps {
  accept: string;
  multiple?: boolean;
  maxSize?: number;
  onUpload: (files: File[]) => void;
  description?: string;
  loading?: boolean;
}

const FileUploader: React.FC<FileUploaderProps> = ({
  accept,
  multiple = false,
  maxSize = 10 * 1024 * 1024, // 默认10MB
  onUpload,
  description,
  loading
}) => {
  const props: UploadProps = {
    name: 'file',
    multiple,
    accept,
    disabled: loading,
    beforeUpload: (file, fileList) => {
      if (file.size > maxSize) {
        message.error(`文件大小不能超过${maxSize / 1024 / 1024}MB`);
        return false;
      }
      
      if (multiple) {
        onUpload(fileList);
      } else {
        onUpload([file]);
      }
      return false;
    },
    showUploadList: false,
  };

  return (
    <Dragger {...props}>
      <p className="ant-upload-drag-icon">
        <InboxOutlined />
      </p>
      <p className="ant-upload-text">点击或拖拽文件到此区域</p>
      <p className="ant-upload-hint">
        {description || `支持的文件格式: ${accept}`}
      </p>
    </Dragger>
  );
};

export default FileUploader; 